from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import PersonID
from django.urls import reverse

# Create your views here.

def login(request):
    
    context = {}
    return render(request,'login/login.html',context)

def ch_pswd(request):
    ses = request.session.items()
    print(ses)

    

    #context = {}
    return render(request,'login/ch_pswd.html')#,context)

def ch_pswd_yunsuan(request):
    '''
    'username': ['xxuser'],
    'password': ['as12345678', '2222222222222', '1111111111111']}
    '''
    info = dict(request.POST)
    print(info)
    print(info['username'])
    pswd_list = info['password']
    try:
        person_obj = PersonID.objects.get\
                     (person_name = info['username'][0])
        
    except PersonID.DoesNotExist:
        return HttpResponse('帐号不存在在')

    else:
        if person_obj.person_pswd == pswd_list[0]:
            if pswd_list[1] == pswd_list[2] and \
               16>=len(pswd_list[1])>=6:

                person_obj.person_pswd = pswd_list[1]
                person_obj.save()

                return HttpResponse('密码修改成功')
            else:

                return HttpResponse('请检查你的密码，\
是否相同，或者符合长度规定定')
        elif person_obj.person_pswd == None:
            if pswd_list[1] == pswd_list[2] and \
               person_obj.person_id[-6:] == pswd_list[1]\
               and 16>=len(pswd_list[1])>=6:

                person_obj.person_pswd = pswd_list[1]
                person_obj.save()

                return HttpResponse('密码修改成功')
            else:

                return HttpResponse('请检查你的密码，\
是否相同，或者符合长度规定定')
                
                
        else:
            return HttpResponse('密码或账号有误')
                    

def login_yunsuan(request):

    info = request.POST
    username = info['username']
    pswd = info['password']
    

    try:
        info_obj = PersonID.objects.get(
            person_name = username)
        
    except PersonID.DoesNotExist:
        
        return HttpResponse('用户名不存在，请联系客服小姐姐')
    else:
        if info_obj.person_pswd == None:
            if pswd == info_obj.person_id[-6:]:
                request.session['username'] = username
                request.session['权限'] = '同学'

                
                return HttpResponse(
                '欢迎VIP{}光临'.format(username),
                                )
            else:
                context = {'message':'你输入的帐号或密码不正确'}
                return render(request,'login/login.html',context)
                
            
            
        elif pswd == info_obj.person_pswd:
            request.session['username'] = username
            request.session['权限'] = '同学'

        
            return HttpResponse(
                '欢迎VIP{}光临'.format(username),
                                )
        else:
            #return HttpResponseRedirect(reverse('login:login'))

            context = {'message':'你输入的帐号或密码不正确'}
            return render(request,'login/login.html',context)


















